;;; *************************************************************************
;;;; FILE IDENTIFICATION
;;;;
;;;; Name:          track.lisp
;;;; Purpose:       Audioscrobbler/lastfm Track API.
;;;; Programmer:    Nicolas Lamirault <nicolas.lamirault@gmail.com>
;;;;
;;;; This file, part of audioscrobbler, is Copyright (c) 2007 by Nicolas Lamirault
;;;;
;;;; cl-audioscrobbler users are granted the rights to distribute and use this software
;;;; as governed by the terms of the MIT License :
;;;; http://www.opensource.org/licenses/mit-license.php
;;;;
;;;; *************************************************************************


(in-package :cl-audioscrobbler)


(defun retrieve-track-fans (artist track)
  "Profiles who've listened to a given track the most, i.e. the biggest fans."
  (get-xml-answer (format nil +track-fans+ artist track)))


(defun retrieve-track-tags (artist track)
  "The most popular tags applied to a given track."
  (get-xml-answer (format nil +track-tags+ artist track)))


;; ----------------
;; API WebServices
;; ----------------


(defun get-track-fans (artist track)
  "A list of profiles who've listened to a given TRACK the most,
i.e. the biggest fans."
  (let ((xmldoc (retrieve-track-fans artist track)))
   (when (and (listp (cdr xmldoc))
              (listp (cadr xmldoc)))
     (loop for sexp in (cdr xmldoc)
        as name = (third (first sexp))
        as url = (second (second sexp))
        as image = (second (third sexp))
        as weight = (second (fourth sexp))
          collect (make-instance 'fan :name name
                                      :url url
                                      :image image
                                      :weight weight)))))


(defun get-track-tags (artist track)
  "A list of the most popular tags applied to a given TRACK."
  (let ((xmldoc (retrieve-track-tags artist track)))
    (when (and (listp (cdr xmldoc))
               (listp (cadr xmldoc)))
      (loop for sexp in (cdr xmldoc)
         as name = (second (second sexp))
         as count = (second (third sexp))
         as url = (second (fourth sexp))
         collect (make-instance 'tag :name name
                                     :count count
                                     :url url)))))